﻿Public Class frmBaoCao

    Sub LoadDanhSach()
        Dim _bc As DataTable
        _bc = BaoCaoDAO.SelectAll()
        dgvBaoCao.DataSource = _bc
    End Sub

    Sub DisplayXEM()
        If (dgvXEMBaoCao.SelectedRows.Count > 0) Then
            cboHOTEN.SelectedValue = dgvXEMBaoCao.SelectedRows(0).Cells(0).Value.ToString()
            txtTONGNGAYDAY.Text = dgvXEMBaoCao.SelectedRows(0).Cells(2).Value.ToString()
            txtSOTIET.Text = dgvXEMBaoCao.SelectedRows(0).Cells(3).Value.ToString()
        End If
    End Sub

    Sub DisplayBC()
        If (dgvBaoCao.SelectedRows.Count > 0) Then
            txtMABC.Text = dgvBaoCao.SelectedRows(0).Cells(0).Value.ToString()
            cboHOTEN.SelectedValue = dgvBaoCao.SelectedRows(0).Cells(1).Value.ToString()
            txtTONGNGAYDAY.Text = dgvBaoCao.SelectedRows(0).Cells(2).Value.ToString()
            txtSOTIET.Text = dgvBaoCao.SelectedRows(0).Cells(3).Value.ToString()
            cboTHANHTICH.SelectedValue = dgvBaoCao.SelectedRows(0).Cells(4).Value.ToString()
        End If
    End Sub

    Public Sub LoadDS()
        Dim _dt As New DataTable
        _dt = GiaoVienDAO.SelectAll()
        cboHOTEN.DataSource = _dt
        cboHOTEN.DisplayMember = "HoTen"
        cboHOTEN.ValueMember = "MaGV"
        cboHOTEN.SelectedIndex = -1

        Dim _tt As New DataTable
        _tt = ThanhTichDAO.SelectAll()
        cboTHANHTICH.DataSource = _tt
        cboTHANHTICH.DisplayMember = "TenThanhTich"
        cboTHANHTICH.ValueMember = "MaThanhTich"
    End Sub
    Private Sub frmBaoCao_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDS()
        LoadDanhSach()
    End Sub

    Private Sub btnXEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXEM.Click
        Dim _maGV As Integer = cboHOTEN.SelectedValue
        Dim _strWhere As String = String.Empty
        Dim strSQL As String
        If _maGV <> 0 Then
            _strWhere += " and GV.MaGV like " & _maGV
            strSQL = "select GV.MaGV, GV.HoTen, count(PC.NgayDay) As NgayDay, Sum(PC.SoTiet) as SoTiet from GIAOVIEN GV, PHANCONG PC where GV.MaGV=PC.MaGV " & _strWhere & " group by GV.MaGV, GV.HoTen"
            Dim _tc As DataTable
            _tc = PhanCongDAO.SelectByGV(strSQL)
            dgvXEMBaoCao.DataSource = _tc
        End If
    End Sub

    Private Sub dgvXEMBaoCao_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvXEMBaoCao.CellContentClick
        DisplayXEM()
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click
        If (dgvXEMBaoCao.SelectedRows.Count > 0) Then
            Dim _bc As BaoCaoDTO = New BaoCaoDTO()
            _bc.MaGV = cboHOTEN.SelectedValue
            _bc.TongSoTiet = txtSOTIET.Text
            _bc.TongNgayDay = txtTONGNGAYDAY.Text
            _bc.MaThanhTich = cboTHANHTICH.SelectedValue
            Try
                BaoCaoDAO.Insert(_bc)
                MessageBox.Show("Đã thêm thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub dgvXEMBaoCao_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvXEMBaoCao.SelectionChanged
        DisplayXEM()
    End Sub

    Private Sub dgvBaoCao_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvBaoCao.CellContentClick
        DisplayBC()
    End Sub

    Private Sub dgvBaoCao_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvBaoCao.SelectionChanged
        DisplayBC()
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _maBC As BaoCaoDTO = New BaoCaoDTO()
        _maBC.MaBC = txtMABC.Text
        Try
            BaoCaoDAO.Delete(_maBC)
            MessageBox.Show("Xóa thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvBaoCao.SelectedRows.Count > 0) Then
            Dim _bc As BaoCaoDTO = New BaoCaoDTO()
            _bc.MaBC = txtMABC.Text
            _bc.MaGV = cboHOTEN.SelectedValue
            _bc.TongSoTiet = txtSOTIET.Text
            _bc.TongNgayDay = txtTONGNGAYDAY.Text
            _bc.MaThanhTich = cboTHANHTICH.SelectedValue
            Try
                BaoCaoDAO.Update(_bc)
                MessageBox.Show("Đã cập nhật thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub

    Private Sub frmBaoCao_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvBaoCao.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _bc As BaoCaoDTO = New BaoCaoDTO()
                _bc.MaBC = txtMABC.Text
                _bc.MaGV = cboHOTEN.SelectedValue
                _bc.TongSoTiet = txtSOTIET.Text
                _bc.TongNgayDay = txtTONGNGAYDAY.Text
                _bc.MaThanhTich = cboTHANHTICH.SelectedValue
                Try
                    BaoCaoDAO.Update(_bc)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
                LoadDanhSach()
            End If
        End If
    End Sub

    Private Sub btnIN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIN.Click
        Dim _dt As New DataTable
        _dt.Clear()
        _dt = BaoCaoDAO.SelectAll()
        dgvBaoCao.DataSource = _dt
        'Nap du lieu cho Report
        Dim myReport As New CR_BaoCao()
        myReport.SetDataSource(_dt.DataSet)
        'Nap form in report
        Dim f As New InBaoCao
        f.CRV_BaoCao.ReportSource = myReport
        f.ShowDialog()
    End Sub
End Class